#!/usr/bin/python
# -*- coding: UTF-8 -*-

import configparser
import os


class ReadConfig:
    """定义一个读取配置文件的类"""

    def __init__(self, filepath=None):
        if filepath:
            configpath = filepath
        else:
            dirname = os.path.dirname(__file__)
            #获取上级目录
            root_dir = os.path.abspath(os.path.join(dirname,".."))
            configpath = os.path.join(root_dir, "config.ini")
        self.cf = configparser.ConfigParser()
        self.cf.read(configpath)
    
    def get_sec_db(self,section,param):
        if self.cf.has_option(section,param): 
            return self.cf.get(section, param)
        return None
        

    def get_sec(self, section):
        if self.cf.has_section(section):
            return self.cf.items(section)


if __name__ == '__main__':
    test = ReadConfig()
    t = test.get_sec_db("source-database","host")
    print(t)
    sec = test.get_sec("source-database")
    print(sec)
